package com.liyao.storehouse.controller;

import com.liyao.storehouse.pojo.menu.MenuType;
import com.liyao.storehouse.service.jumpService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: storehouse
 * @description:
 * @author: liu yan
 * @create: 2024-11-13 19:41
 */
@Controller
public class jumpController {
    @Resource
    jumpService service;

    @RequestMapping("/")
    public String login() {
        return "/pages/login";
    }

    @RequestMapping("/out")
    public String out(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }

    @RequestMapping("/index")
    public String index(Model model, HttpSession session) {
        if (null == session.getAttribute("user_id")) {
            return "redirect:/";
        }
        List<MenuType> list = service.getMenuList((Integer) session.getAttribute("user_id"), session);
        model.addAttribute("menuList", list);
        return "pages/index";
    }

    @RequestMapping("/jump/user")
    public String User(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/userCenter/user";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/role")
    public String role(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/userCenter/role";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/control")
    public String control(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/control";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/StorageActivation")
    public String StorageActivation(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/StorageActivation";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/storage")
    public String storage(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/storageCenter/storage";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/outbound")
    public String outbound(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/storageCenter/outbound";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/check")
    public String check(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/storageCenter/check";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/storeList")
    public String storeList(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/storageCenter/storeList";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/supplies")
    public String supplies(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/suppliesCenter/supplies";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/receiveLog")
    public String receiveLog(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/intelligentCenter/receiveLog";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/goodsList")
    public String goodsList(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/intelligentCenter/goodsList";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/inOutLog")
    public String inOutLog(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/intelligentCenter/inOutLog";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/loginLog")
    public String loginLog(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/logCenter/loginLog";
        }
        return "redirect:/out";
    }

    @RequestMapping("/jump/usedLog")
    public String usedLog(HttpSession session, String id, Model model) throws Exception {
        if (null != session.getAttribute("user_id")) {
            getQX(session, id, model);
            return "pages/logCenter/usedLog";
        }
        return "redirect:/out";
    }


    private void getQX(HttpSession session, String id, Model model) {
        String user = session.getAttribute("user_id").toString();
        String roles = session.getAttribute("roles").toString();
        System.out.println(roles.contains(id + "_edit") ? 1 : 0);
        model.addAttribute("add", roles.contains(id + "_add") ? 1 : 0);
        model.addAttribute("edit", roles.contains(id + "_edit") ? 1 : 0);
        model.addAttribute("delete", roles.contains(id + "_delete") ? 1 : 0);
        model.addAttribute("small", roles.contains(id + "_small") ? 1 : 0);
    }
}
